package com.dingding.special.common.facade.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.dingding.common.core.beans.UserTaxiOrdert;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.common.core.cache.base.JedisUtil;
import com.dingding.common.core.cache.enums.DbEnum;
import com.dingding.facade.queue.facade.IQueueFacade;
import com.dingding.facade.special.ISpecialOrderFacade;
import com.dingding.facade.special.trasfer.place.ReqBackstagePlace;
import com.dingding.facade.special.trasfer.place.ResBackstagePlace;
import com.dingding.facade.special.trasfer.route.RouteDto;
import com.dingding.special.order.appoint.biz.IAppointOrderService;
import com.dingding.special.order.appoint.biz.IAppointSpecialOrderService;
import com.dingding.special.order.appoint.delayappoint.IDelayOrderHolder;
import com.dingding.special.order.appoint.delayappoint.impl.DelayOrderHolder;
import com.dingding.special.order.publish.biz.IPublishSpecialOrder;

@Component("specialOrderFacade")
public class SpecialOrderFacadeImpl implements ISpecialOrderFacade {

	@Resource(name = "appointRealOrderService")
	private IAppointSpecialOrderService appointRealOrderService;

	@Resource(name = "appointUnRealOrderService")
	private IAppointSpecialOrderService appointUnRealOrderService;

	@Resource(name = "appointRealTaxiOrderService")
	private IAppointOrderService appointRealTaxiOrderService;

	@Resource(name = "publishSpecialOrderBackstage")
	private IPublishSpecialOrder publishSpecialOrder;

	@Resource(name = "tcpAppointRealOrder")
	private IAppointSpecialOrderService tcpAppointRealOrder;

	@Resource(name = "tcpAppointUnRealOrder")
	private IAppointSpecialOrderService tcpAppointUnRealOrder;

	@Resource(name = "tcpAppointTaxiOrder")
	private IAppointOrderService tcpAppointTaxiOrder;

	@Resource(name = "delayOrderHolder")
	private IDelayOrderHolder delayOrderHolder;
	
	@Resource(name = "queueFacade")
	private IQueueFacade queueFacade;
	
	@Override
	public boolean appointRealOrder(final UserspecialorderT userspecialorderT) {
		if (JedisUtil.isExistKey(userspecialorderT.getSpecialOrderId(), DbEnum.AppointOrderQueue.getValue())) {
			delayOrderHolder.appointDelayOrder(
					DelayOrderHolder.createTimerTask(
							queueFacade,appointRealOrderService, userspecialorderT));
		} else {
			JedisUtil.setValue(userspecialorderT.getSpecialOrderId(),
					String.valueOf(userspecialorderT.getOrderStatus()), DbEnum.AppointOrderQueue.getValue());
			delayOrderHolder.appointOrder(
					DelayOrderHolder.createTimerTask(
							queueFacade,appointRealOrderService, userspecialorderT));
		}
		return true;
	}

	@Override
	public boolean appointUnRealOrder(final UserspecialorderT userspecialorderT) {
		if (JedisUtil.isExistKey(userspecialorderT.getSpecialOrderId(), DbEnum.AppointOrderQueue.getValue())) {
			delayOrderHolder.appointDelayOrder(
					DelayOrderHolder.createTimerTask(
							queueFacade,appointUnRealOrderService, userspecialorderT));
		} else {
			JedisUtil.setValue(userspecialorderT.getSpecialOrderId(),
					String.valueOf(userspecialorderT.getOrderStatus()), DbEnum.AppointOrderQueue.getValue());
			delayOrderHolder.appointOrder(
					DelayOrderHolder.createTimerTask(
							queueFacade,appointUnRealOrderService, userspecialorderT));
		}
		return true;
	}

	@Override
	public boolean appointTaxiOrder(final UserTaxiOrdert taxiOrdert) {
		if (JedisUtil.isExistKey(taxiOrdert.getTaxiOrdertId(), DbEnum.AppointOrderQueue.getValue())) {
			delayOrderHolder.appointDelayOrder(
					DelayOrderHolder.createTimerTask(
							queueFacade,appointRealTaxiOrderService, taxiOrdert));
		} else {
			JedisUtil.setValue(taxiOrdert.getTaxiOrdertId(),
					String.valueOf(taxiOrdert.getOrderStatus()), DbEnum.AppointOrderQueue.getValue());
			delayOrderHolder.appointOrder(
					DelayOrderHolder.createTimerTask(
							queueFacade,appointRealTaxiOrderService, taxiOrdert));
		}
		return true;
	}

	@Override
	public boolean appointTcpRealOrder(UserspecialorderT userspecialorderT) {
		return tcpAppointRealOrder.appointSpecialOrder(userspecialorderT);
	}

	@Override
	public boolean appointTcpUnRealOrder(UserspecialorderT userspecialorderT) {
		return tcpAppointUnRealOrder.appointSpecialOrder(userspecialorderT);
	}

	@Override
	public boolean appointTcpTaxiOrder(UserTaxiOrdert taxiOrdert) {
		return tcpAppointTaxiOrder.appointOrder(taxiOrdert);
	}

	@Override
	public ResBackstagePlace placeSpecialOrderBackstage(ReqBackstagePlace reqBackstagePlace) {
		return this.publishSpecialOrder.placeSpecialOrderBackstage(reqBackstagePlace);
	}

	@Override
	public List<RouteDto> getRoutes(String depId) {
		return this.publishSpecialOrder.getRoutes(depId);
	}

}
